import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import AdminHome from '../components/AdminHome.vue'
import VipUserHome from '../components/VipUserHome.vue'
import adminvipuser from '../components/adminvipuser.vue'
import VipInformation from '../components/VipInformation.vue'
import Notice from '../components/Notice.vue'
import Notices from '../components/Notices.vue'
Vue.use(VueRouter)
const routes = [
  {path: '/', redirect: '/login'},
  {path: '/login', component:Login},
  {path: '/adminhome', component:AdminHome,children:[
    {
      path:'/notice', component:Notice
    },
    {
      path:'/vipuser', component:adminvipuser
    }

  ]
  },

  {path:'/vipuserhome', component:VipUserHome,
    children:[
    {
      path: '/vipinformation', component:VipInformation
    },
    {
      path: '/notice/vip', component:Notices
    }
    ]
  }
]

const router = new VueRouter({
  routes
})
/**
 * 定义路由守卫 拦截用户所有请求 需要定义一个回调函数
 * 三个参数   to:要访问的请求路径  from:从哪个页面跳转过来  next 表示放行
 * 需求:
 * 用户访问login  方行
 * 如果不是login 判断有没有token  没有则退回登录页面
 * */

router.beforeEach((to,from,next)=>{
  if(to.path=='/login'){
    return next()
  }else{
    let token= window.sessionStorage.getItem("token")
    if(!token){
      return next('/login')
    }else{
      return next();
    }
  }


})

export default router
